<?php

namespace app\admin\controller;


use think\Request;

class Detail extends Base
{
    //
    //新鲜炒菜列表
    public function all()
    {
        //查询内容
        $details = model('Detail')->with('dish')->order('create_time','desc')->paginate(10);
        //模板变量
        $viewData = [
            'details' => $details
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //新鲜炒菜添加显示
    public function index()
    {
        $dishs = model('Dish')->select();
        //所查询导航给模板变量
        $viewData = [
            'dishs' => $dishs
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //新鲜炒菜添加
    public function add(Request $request)
    {
        $data = $request->param();
        $validate = new \app\common\validate\Detail();
        if (!$validate->check($data)){
            return ['code' => 0,'msg' => $validate->getError()];
        }
        //创建模型
        $db = new \app\common\model\Detail();
        //_update接收data
        $result = $db->_update($data);
        if ($result){
            return ['code' => 1,'msg' => '操作成功'];
        }else{
            return ['code' => 0,'msg' => '操作失败，请稍后再试！'];
        }
    }

    //新鲜炒菜编辑
    public function edit(Request $request)
    {
        //接收id
        $id = $request->param('id');
        //实例化模型
        $db = new \app\common\model\Detail();
        //查询轮播id
        $detailInfo = $db->where('id',$id)->find();
        $dishs = model('Dish')->select();
        //模板变量
        $viewData = [
            'detailInfo' => $detailInfo,
            'dishs' => $dishs
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //新鲜炒菜删除
    public function del()
    {
        $dishInfo = model('Detail')->find(input('post.id'));
        $result = $dishInfo->delete();
        if ($result){
            $this->success('删除成功','admin/detail/all');
        }else{
            $this->error('删除失败');
        }
    }
}
